﻿//功能描述：系统设置-机构设置

using Hong.Model.Dto.PesponseDto.ResponseModel.ResultModel;
using Hong.Model.SysDomain.Model_SystemSettings;
using Hong.Repository.Data;
using Hong.Common.Expressions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Hong.IRepository.IRepository_SystemSettings;
using Hong.Model.SysDomain.Model_SystemSettings.ViewModel;

namespace Hong.Repository.Repository.Repository_SystemSettings
{
    public class DepartmentDal : DataBase, IDepartmentDal
    {
        #region 构造器注入

        private readonly T_HongShi_3Context _t_HongShi_3Context;

        public DepartmentDal(T_HongShi_3Context t_HongShi_3Context) : base(t_HongShi_3Context)
        {
            _t_HongShi_3Context = t_HongShi_3Context;
        }

        #endregion


        #region 部门管理

        #region 加载部门(分页/模糊查询)
        public async Task<PageResponse<Dep_DicModel>> GetDep(string DepName, int pageindex, int pagesize)
        {

            Expression<Func<Dep_DicModel, bool>> userExpression = sysUser => true;

            var sql = from a in _t_HongShi_3Context.Departments
                      join b in _t_HongShi_3Context.Dictionaries on a.DepType equals b.DicId 
                      join c in _t_HongShi_3Context.Departments on a.DepSuperior equals c.DepId into dicdep
                      from d in dicdep.DefaultIfEmpty()
                      select new Dep_DicModel
                      {
                          DepId = a.DepId,
                          DepDescribe = a.DepDescribe,
                          DepSuperiorNmae = d.DepName,
                          DepSort = a.DepSort,
                          DepName = a.DepName,
                          DicId = b.DicId,
                          DicName = b.DicName,
                          DicCode = b.DicCode,
                      };

            if (!string.IsNullOrEmpty(DepName))
            {
                userExpression =  userExpression.And(c => c.DepName.Contains(DepName));
            }

            
                var query = await this.LoadPageEntitiesAsync(sql.Where(userExpression), pageindex, pagesize, true, a => (int)a.DepSort);
                return query;
            
        }
        #endregion

        #region 部门添加
        public async Task<ResponseModel<bool>> AddDep(Department department)
        {
            var result = new ResponseModel<bool>();
            int count;

            await AddAsync(department);
            count = await SaveChangesAsync();
            return count > 0 ? result.Succeed(true) : result.Fail("操作失败");
        }
        #endregion

        #region 加载部门下拉列表
        public async Task<ResponseModel<List<Department>>> selecDep()
        {
            var result = new ResponseModel<List<Department>>();
            var list = await this.LoadEntitiesAsync<Department>();
            return result.Succeed(list);
        }
        #endregion

        #region 公共查询字典表
        public async Task<ResponseModel<List<Dictionary>>> selecDic(string DicCode)
        {
            var result = new ResponseModel<List<Dictionary>>();
            var list = await this.LoadEntitiesAsync<Dictionary>(u=>u.DicCode== DicCode);
            return result.Succeed(list);
        }
        #endregion

        #region 修改部门隐藏显示
        public ResponseModel<bool>UpHide(int DepId)
        { 
            var result=new ResponseModel<bool>();
            return null;
        }
        #endregion

        #endregion



    }
}
